package ua.kpi_java_training.tyshchenko.module2;

import java.util.LinkedList;

/**
 * Class contains methods which makes the movement of fish, murder victims
 *
 * @author Vlad Tyshchenko
 * @version 1.1 3 June 2013
 */
public class Life {
    private int amountCycle;
    private Statistics statistics;
    private ListCreature listVictim, listPredator;

    /**
     * The constructor gets amount cycle in program, amount victims and predatros.
     * Creates object Habitat which contains size of habitat.
     * Creates object Statistics.
     * Creates list of victims and predators.
     *
     * @param habitat
     */
    public Life(Habitat habitat, InputAllData allData) {
        int amountVictim, amountPredator;
        amountCycle = allData.getAmountCycle();
        amountVictim = allData.getAmountVictim();
        amountPredator = allData.getAmountPredator();
        statistics = new Statistics(amountVictim, amountPredator);
        listVictim = new ListCreature(new Victim(habitat), amountVictim);
        listPredator = new ListCreature(new Predator(habitat), amountPredator);
    }

    /**
     * Method does all creatures one move.
     *
     * @param listCreature list of creatures which move
     */
    private void stepAllFish(ListCreature listCreature) {
        int amountCreature = listCreature.size();
        for (int i = 0; i < amountCreature; i++) {
            CreatureInHabitat creature = listCreature.getCreature(i);
            creature.move();
            if (creature.checkBirth()) {
                statistics.addChildren(creature.getKeyCreature());
                listCreature.add(creature.getNewFish());
            }
        }
    }

    /**
     * If a predator is on the same site with victim - victim dies
     *
     * @param predator
     */
    private void killByOnePredator(CreatureInHabitat predator) {
        int amountVictim = listVictim.size();
        for (int i = 0; i < amountVictim; i++) {
            CreatureInHabitat victim = listVictim.getCreature(i);
            if (predator.getPosition().equals(victim.getPosition())) {
                statistics.addKilledVictim();
                listVictim.remove(victim);
                predator.addCountNewLife();
                amountVictim = listVictim.size();
            }
        }
    }

    /**
     * Enumerates all list of predators to perform the method 'killByOnePredator'
     */
    private void killByAllPredators() {
        int amountPredator = listPredator.size();
        for (int i = 0; i < amountPredator; i++) {
            killByOnePredator(listPredator.getCreature(i));
        }
    }

    /**
     * Method does all cycle.
     * One cycle contains move victim and the predator, murder victims.
     */
    public void flowingOfLife() {
        for (int i = 0; i < amountCycle; i++) {
            stepAllFish(listVictim);
            stepAllFish(listPredator);
            killByAllPredators();
        }
    }

    public String getStatistics() {
        return statistics.toString();
    }

}
